package com.market.member.service.login;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.model.dto.LoginReqDTO;
import com.block.common.service.IAppLoginInfoService;
import com.block.common.service.IMemberCommonService;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppLoginInfo;
import com.block.db.entity.MemberInfo;
import com.block.db.mapper.MemberInfoMapper;
import com.block.db.util.IdTypeEnum;
import com.block.db.util.SnowflakeUtil;
import com.block.sms.dto.AuthResultDTO;
import com.block.sms.service.factory.QuickLoginFactory;
import com.market.member.model.dto.LoginResultDTO;
import com.market.member.service.ILoginTypeService;
import com.market.member.service.IMemberInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 一键登录
 */
@Slf4j
@Service
public class AuthLoginServiceImpl extends ILoginTypeService {

    @Autowired
    IMemberInfoService memberInfoService;
    @Resource
    MemberInfoMapper memberInfoMapper;
    @Autowired
    IMemberCommonService memberCommonService;
    @Autowired
    IAppLoginInfoService appLoginInfoService;

    @Override
    public ResponseVo toLogin(LoginReqDTO loginReqDTO) {

        JSONObject tokenJson = new JSONObject();
        tokenJson.put("loginToken", loginReqDTO.getLoginCode());

        String appCode = loginReqDTO.getAppInfo().getAppCode();
        AppLoginInfo appLoginInfo = appLoginInfoService.getEntityInfoByAppCode(appCode);

        String outId = SnowflakeUtil.getSnowflakeByDate(IdTypeEnum.COMMON,30);
        ResponseVo responseVo = QuickLoginFactory.getService(appLoginInfo.getPlatformType()).getMobile(outId, tokenJson, appLoginInfo);
        if (!AppRspCodeConstant.SUCCESS.equals(responseVo.getRspCode())) {
            return responseVo;
        }

        AuthResultDTO resultDTO = responseVo.getData().toJavaObject(AuthResultDTO.class);

        String mobile = resultDTO.getPhone();

        MemberInfo memberInfo = memberInfoMapper.getBaseUserInfoByPhoneLock(mobile);
        boolean isNewMember = false;
        if (memberInfo == null){
            memberInfo = memberCommonService.saveRegisterMemberInfo(loginReqDTO.getClientType(), mobile,
                    loginReqDTO.getRegisterChannel(), loginReqDTO.getAppInfo(), loginReqDTO.getReqIp(),  null, loginReqDTO.getZhima());
            if (ObjectUtil.isEmpty(memberInfo)){
                log.error("【验证码登录】创建用户失败");
                return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "登录失败，请重试","");
            }
            isNewMember = true;
        }else{
            MemberInfo updateMember = new MemberInfo();
            updateMember.setUid(memberInfo.getUid());
            updateMember.setZhima(loginReqDTO.getZhima());
            memberInfoMapper.updateById(updateMember);
        }

        // 登录结果
        LoginResultDTO loginResultDTO = new LoginResultDTO();
        loginResultDTO.setUid(memberInfo.getUid());
        loginResultDTO.setNewMember(isNewMember);
        loginResultDTO.setMemberType(memberInfo.getType());

        return new ResponseVo(AppRspCodeConstant.SUCCESS, loginResultDTO);
    }
}
